<?php

require_once("session.php");
require_once("connect_to_database.php");

// Removing notes that are older than an hour:
//mysql_query("DELETE FROM notes WHERE id>2 AND dt<SUBTIME(NOW(),'0 1:0:0')");

$project_name = $_POST['prname'];

//find out whether the user is a chicken in this project or not
//if so, then we won't trigger the make_draggable function that allows
//the user to drag cards around
$query = sprintf("select isChicken from projects where name='%s' and participant='%s'", $project_name, $_SESSION['usr']);
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$chicken = $row['isChicken'] == "1" ? true : false;



$query = mysql_query("SELECT * FROM `$project_name` ORDER BY id DESC");
$notes = '';
$left='';
$top='';
$zindex='';

if(!$query)
		die($query);

while($row=mysql_fetch_assoc($query))
{
	// The xyz column holds the position and z-index in the form 200x100x10:
	list($left,$top,$zindex) = explode('x',$row['xyz']);


	echo ('
	<div class="note '.$row['color'].'" style="left:'.$left.'px;top:'.$top.'px;z-index:'.$zindex.'">
	  '. (!$chicken ? '<a id="launch'.$row['id'].'" href="functions/get_editable_note.php"><img class="note_edit" width="20px" height="20px" src="images/edit.png" /></a>' : '' ) .'
		<div class="author">'.htmlspecialchars($row['name']).'</div>
		<div style="width:100%; height:16px"></div>
		'.htmlspecialchars($row['text']).'
		<span class="data">'.$row['id'].'</span>
	</div>
	');	


	?>

	<script type="text/javascript">
		var headers = {id: "<?php echo $row['id'] ?>" , project: "<?php echo $_POST['prname'] ?>" };
		//Cause the "edit" buttons to launch a new edit screen in a fancy box	
		$("#launch<?php echo $row['id'] ?>").fancybox({
			"height"				: 300,
		"autoDimensions": false,
			"zoomSpeedIn"		: 600,
			"zoomSpeedOut"		: 500,
			"easingIn"			: "easeOutBack",
			"easingOut"			: "easeInBack",
			"hideOnContentClick": false,
			"padding"			: 15,
			ajax: {
				type : "POST",
				data :  headers
				}
		});
	
	</script>
	
<?php



}


//support auto refresh and card dragging (for non chickens)
echo ('<script type="text/javascript">
		var board_timeout = startBoardRefresh();
		'.(!$chicken ? "make_draggable($('.note'));" : "") .'
		
		

	</script>');

?>
